package com.xyu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.xyu.gulimall.product.entity.AttrEntity;
import com.xyu.gulimall.product.entity.CategoryEntity;
import com.xyu.gulimall.product.service.AttrAttrgroupRelationService;
import com.xyu.gulimall.product.service.AttrService;
import com.xyu.gulimall.product.service.CategoryService;
import com.xyu.gulimall.product.vo.AttrGroupRelationVo;
import com.xyu.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xyu.gulimall.product.entity.AttrGroupEntity;
import com.xyu.gulimall.product.service.AttrGroupService;
import com.xyu.common.utils.PageUtils;
import com.xyu.common.utils.R;


/**
 * 属性分组
 *
 * @author wangyufei
 * @email 2070522113@qq.com
 * @date 2021-05-03 21:27:00
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 属性分组关联，发送请求/product/attrgroup/{attrgroupId}/attr/relation
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        //查询属性分组表
        List<AttrEntity> attrEntities =  attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",attrEntities);
    }
    /**
     * 添加分组属性与分组关联关系
     * 请求地址：/product/attrgroup/attr/relation
     * 请求参数 [{
     *   "attrGroupId": 0, //分组id
     *   "attrId": 0, //属性id}]
     */
    @PostMapping("/attr/relation")
    public  R addAttrGroup(@RequestBody List<AttrGroupRelationVo> vos){
        //保存操作
        relationService.saveBatch(vos);

        return R.ok();
    }

    /**
     * 请求地址：/product/attrgroup/{attrgroupId}/noattr/relation
     * return 获取当前分组没有关联的所以属性
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String,Object> params){
        //查询属性分组表 参数为分页信息，条件信息 和分组id
        PageUtils page =  attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }

    /**移除属性
     * 请求地址 /product/attrgroup/attr/relation/delete
     * 返回参数 [{"attrId":1,"attrGroupId":2}]
     */
    @PostMapping("/attr/relation/delete")
    public R RelationDelete(@RequestBody AttrGroupRelationVo[] vos){
        for (AttrGroupRelationVo vo : vos) {
            System.out.println("vos"+vo);
        }
        attrService.deleteRelation(vos);
        return R.ok();
    }
    /**
     * 列表【增强查询列表】
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils pageUtils = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", pageUtils);
    }


    /**
     * 信息
     * 回显信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        //查询路径，[因为回显的是多级菜单所以需要单独查询]
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        List<Long> cateLogPath = categoryService.findCateLogPath(catelogId);
        attrGroup.setCatelogPath(cateLogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }
    /**
     * 17，获取分类下所有分组&关联属性
     * 请求地址：/product/attrgroup/{catelogId}/withattr
     */

    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        //1，查询当前分类下的所属分组
        //2，查询每个分组的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }

}
